package rps.client.ui;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JPanel;

import rps.client.ArrayShuffle;
import rps.client.UIController;
import rps.game.data.FigureKind;

public class InitialAssignmentPanel extends JPanel implements ActionListener{
	
	private static final long serialVersionUID = -3659355705428630780L;
	private GamePane gamePane;	
	
	private JButton allRandomButton = new JButton("Zufall"),
			moveableRandomButton = new JButton("<html>Zufall <br> außer Flagge und Falle</html>"),
			clearButton = new JButton("Neu");
	
	public InitialAssignmentPanel(GamePane gamePane){
		super();
		this.gamePane = gamePane;
		initComponents();
	}
	
	/**
	 * Sets the Layout of the initialAssignmentPanel and adds the buttons.
	 */
	private void initComponents(){
		setLayout(new GridLayout(1,3));
		add(allRandomButton);
		allRandomButton.addActionListener(this);
		add(moveableRandomButton);
		moveableRandomButton.addActionListener(this);
		add(clearButton);
		clearButton.addActionListener(this);
		
	}
	
	/**
	 * Determine in which Phase of the game it actually is,
	 * so that Buttons respond different depending on the placementphase.
	 * Enables / disables needed / unneeded components.
	 * @param p
	 */
	public void setPlacementPhaseWithEnabling(int p){
		//flag not set
		if(p==UIController.FLAGNOTSET){
			gamePane.uiController.setPlacementPhase(0);
			enable(true, false, true);
			gamePane.enableWeaponChoicePanel(false);
			gamePane.setStatusText("Flagge setzen!");
		}
		//trap not set
		else if(p==UIController.TRAPNOTSET){
			gamePane.uiController.setPlacementPhase(1);
			enable(true, false, true);
			gamePane.enableWeaponChoicePanel(false);
			gamePane.setStatusText("Falle setzen!");
		}
		//all set
		else if(p==UIController.ALLSET){
			gamePane.uiController.setPlacementPhase(2);
			enable(true);
			gamePane.enableWeaponChoicePanel(true);
			gamePane.setStatusText("Wählen Sie eine Figur um zu bestimmen wer beginnt!");
			
		}
		//game started
		else if(p==UIController.GAME){
			gamePane.uiController.setPlacementPhase(-1);
		}
		else{
			//should never happen
		}
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource().equals(allRandomButton)){
			generateRandomAssignment("all");
			setPlacementPhaseWithEnabling(UIController.ALLSET);
		}
		else if(e.getSource().equals(moveableRandomButton)){
			generateRandomAssignment("moveable");
			setPlacementPhaseWithEnabling(UIController.ALLSET);
		}
		else if(e.getSource().equals(clearButton)){
			gamePane.uiController.gameController.resetAssignment();
			setPlacementPhaseWithEnabling(UIController.FLAGNOTSET);
		}
	}
	
	/**
	 * Enables the initial Assignment Buttons.
	 */
	@Override
	public void enable(boolean enable){
		allRandomButton.setEnabled(enable);
		moveableRandomButton.setEnabled(enable);
		clearButton.setEnabled(enable);
	}	
	public void enable(boolean b1, boolean b2, boolean b3){
		allRandomButton.setEnabled(b1);
		moveableRandomButton.setEnabled(b2);
		clearButton.setEnabled(b3);
	}
	
	/**
	 * Generates a fully random Assignment or just random moveables.
	 * At the end pass it to the gameController which updates the board.
	 * @param s "all" for random all figure or "moveable" for only moveables.
	 */
	public void generateRandomAssignment(String s){
		FigureKind[] preAssignment = new FigureKind[14];

		FigureKind[] defaultAssignment = new FigureKind[]{
				FigureKind.ROCK,
				FigureKind.ROCK,
				FigureKind.ROCK,
				FigureKind.ROCK,
				FigureKind.PAPER,
				FigureKind.PAPER,
				FigureKind.PAPER,
				FigureKind.PAPER,
				FigureKind.SCISSORS,
				FigureKind.SCISSORS,
				FigureKind.SCISSORS,
				FigureKind.SCISSORS,
				FigureKind.TRAP,
				FigureKind.FLAG};

		FigureKind[] moveableAssignment = new FigureKind[]{
				FigureKind.ROCK,
				FigureKind.ROCK,
				FigureKind.ROCK,
				FigureKind.ROCK,
				FigureKind.PAPER,
				FigureKind.PAPER,
				FigureKind.PAPER,
				FigureKind.PAPER,
				FigureKind.SCISSORS,
				FigureKind.SCISSORS,
				FigureKind.SCISSORS,
				FigureKind.SCISSORS};
		
		if(s.equals("all")){
			ArrayShuffle.shuffle(defaultAssignment);
			for(int i=0; i<14; i++){
				preAssignment[i] = defaultAssignment[i];
			}
		}
		else if(s.equals("moveable")){
			ArrayShuffle.shuffle(moveableAssignment);
			int index = 0;
			preAssignment = gamePane.getInitialFigures();
			for(int i=0; i<preAssignment.length; i++){
				if(preAssignment[i]==null){
					preAssignment[i] = moveableAssignment[index++];
				}
				else if(preAssignment[i].equals(FigureKind.FLAG) || preAssignment[i].equals(FigureKind.TRAP)){
					//do nothing
				}
				else{
					preAssignment[i] = moveableAssignment[index++];
				}
			}
		}
		else{
			//should not happen
		}		
		gamePane.uiController.gameController.setAssignment(preAssignment);
	}

	public void reset() {
		setPlacementPhaseWithEnabling(UIController.FLAGNOTSET);
	}
}
